package com.zygame.sdk.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zygame.sdk.utils.log.LogHelper;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseDao<T> {

    protected final String VARCHAR = " VARCHAR ";
    protected final String INTEGER = " INTEGER ";
    protected final String LONG = " LONG ";
    protected final String DOUBLE = " DOUBLE ";

    protected abstract void onCreate(SQLiteDatabase db);

    protected abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

    protected abstract T cursor2Model(Cursor cursor);

    protected abstract String getTableName();

    protected abstract ContentValues buildContentValues(T t);

    public long add(T t) {
        return this.getDatabase().insert(this.getTableName(), null, this.buildContentValues(t));
    }

    /**
     * 根据id查询单个对象
     *
     * @param id
     * @return
     */
    public T query(int id) {
        String sql = " SELECT * FROM " + getTableName() + " WHERE _ID=" + id;
        Cursor cursor = this.getDatabase().rawQuery(sql, null);
        while (cursor.moveToNext()) {
            T t = this.cursor2Model(cursor);
            cursor.close();
            return t;
        }
        cursor.close();
        return null;
    }

    /**
     * 根据某个字段查询
     *
     * @param rowName  字段名
     * @param rowValue 字段值
     */
    public T query(String rowName, String rowValue) {
//		String sql = " SELECT * FROM " + getTableName() + " WHERE " + rowName + "=" + "\"" + rowValue + "\"";
        String sql = " SELECT * FROM " + getTableName() + " WHERE " + rowName + "='" + rowValue + "'";
        T t = null;
        try {
            Cursor cursor = this.getDatabase().rawQuery(sql, null);
            while (cursor.moveToFirst()) {
                t = this.cursor2Model(cursor);
                break;
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 查询全表
     *
     * @return
     */
    public List<T> queryList() {
        return this.queryList(" SELECT * FROM " + getTableName());
    }

    /**
     * 通过sql查询
     *
     * @param sql
     * @return
     */
    protected List<T> queryList(String sql) {
        return this.queryList(sql, null);
    }

    /**
     * 通过sql查询
     *
     * @param sql
     * @return
     */
    protected List<T> queryList(String sql, String[] args) {
        SQLiteDatabase db = this.getDatabase();
        Cursor cursor = db.rawQuery(sql, args);
        List<T> list = new ArrayList<T>();
        try {
            while (cursor.moveToNext()) {
                list.add(this.cursor2Model(cursor));
            }
        } catch (Exception e) {
            // Log.e(e);
            LogHelper.e(getTableName(), e);
        } finally {
            cursor.close();
            db.close();
        }

        return list;
    }

    /**
     * 根据id修改对象
     *
     * @param t
     * @param id
     */
    public int update(T t, int id) {
        return this.update(this.buildContentValues(t), "_ID=?", new String[]{id + ""});
    }

    /**
     * 自定义修改条件
     *
     * @param t
     * @param whereClause
     * @param whereArgs
     */
    public int update(T t, String whereClause, String[] whereArgs) {
        return this.update(this.buildContentValues(t), whereClause, whereArgs);
    }

    public int update(ContentValues values, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = this.getDatabase();
        int res = this.getDatabase().update(getTableName(), values, whereClause, whereArgs);
        db.close();
        return res;
    }

    public void execSQL(String sql) {
        SQLiteDatabase db = this.getDatabase();
        db.execSQL(sql);
        db.close();
    }

    public void execSQL(String sql, Object[] bindArgs) {
        SQLiteDatabase db = this.getDatabase();
        db.execSQL(sql, bindArgs);
        db.close();
    }

    /**
     * 根据id(物理)删除
     *
     * @param id
     */
    public int del(int id) {
        // String sql = " DELETE FROM " + getTableName() + " WHERE _ID=" + id;
        // this.getDatabase().execSQL(sql);

        return this.getDatabase().delete(getTableName(), "_ID=?", new String[]{id + ""});
    }

    protected SQLiteDatabase getDatabase() {
        return DBHelper.newInstance().getWritableDatabase();
    }


    public T query(String rowName, String rowValue,String type) {
//		String sql = " SELECT * FROM " + getTableName() + " WHERE " + rowName + "=" + "\"" + rowValue + "\"";
        String sql = " SELECT * FROM " + getTableName() + " WHERE " + rowName + "='" + rowValue + "' AND _TYPE =" +type;
        T t = null;
        try {
            Cursor cursor = this.getDatabase().rawQuery(sql, null);
            while (cursor.moveToFirst()) {
                t = this.cursor2Model(cursor);
                break;
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

}